<html>
<head><meta charset="utf-8"><title>Array pointers · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html">Array pointers</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="172401422"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172401422" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> DutchGhost <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172401422">(Aug 03 2019 at 10:37)</a>:</h4>
<p>I was trying to simplify working with array's that contain MaybeUninit`s, and wrote a simple wrapper.<br>
Here's the wrapper: <a href="https://github.com/DutchGhost/partialarray/blob/master/src/lib.rs" target="_blank" title="https://github.com/DutchGhost/partialarray/blob/master/src/lib.rs">https://github.com/DutchGhost/partialarray/blob/master/src/lib.rs</a></p>
<p>The question is about the <code>first_ptr</code> function on <a href="https://github.com/DutchGhost/partialarray/blob/master/src/lib.rs#L77" target="_blank" title="https://github.com/DutchGhost/partialarray/blob/master/src/lib.rs#L77">https://github.com/DutchGhost/partialarray/blob/master/src/lib.rs#L77</a></p>
<p>However, I wondered; Given <code>N</code> is 0, is the pointer returned from <code>first_ptr</code> and <code>first_ptr_mut</code> valid?<br>
Those function could look like:</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">first_ptr</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="n">N</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">mem</span>::<span class="n">align_of</span>::<span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">T</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="bp">self</span><span class="p">.</span><span class="n">nth_ptr</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>so that for N being 0, an aligned pointer is returned, which can be used by for example <code>slice::from_raw_parts(ptr, 0)</code></p>



<a name="172401477"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172401477" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172401477">(Aug 03 2019 at 10:39)</a>:</h4>
<p><code>nth_ptr</code> is unsound, it can be used for OOB arithmetic</p>



<a name="172401479"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172401479" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172401479">(Aug 03 2019 at 10:39)</a>:</h4>
<p>that is UB completely independent of any initialization concerns</p>



<a name="172401491"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172401491" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172401491">(Aug 03 2019 at 10:40)</a>:</h4>
<p>but I dont see any problem with the 0 case, why would there be a problem there?</p>



<a name="172401528"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172401528" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> DutchGhost <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172401528">(Aug 03 2019 at 10:40)</a>:</h4>
<p>It can be used for OOB arithmatic yeah, however in order to use the pointer, <code>unsafe</code> must be written anyway, which is why I didnt mark it <code>unsafe</code>.</p>



<a name="172401537"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172401537" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172401537">(Aug 03 2019 at 10:41)</a>:</h4>
<p>creating an OOB reference is insta-UB</p>



<a name="172401540"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172401540" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172401540">(Aug 03 2019 at 10:41)</a>:</h4>
<p>and <code>get_unchecked</code> returns a reference</p>



<a name="172401583"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172401583" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172401583">(Aug 03 2019 at 10:42)</a>:</h4>
<p>so it doesnt matter if you dont use the result. OOB with <code>get_unchecked</code> is UB.</p>



<a name="172401585"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172401585" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> DutchGhost <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172401585">(Aug 03 2019 at 10:42)</a>:</h4>
<p>Why it would be a problem? Because Im not sure what it does? [T; 0] doesnt really exist at runtime, so having a ptr to the 0th element...eh? calling <code>array[0]</code> panics for an array of 0 elements</p>



<a name="172401631"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172401631" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> DutchGhost <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172401631">(Aug 03 2019 at 10:44)</a>:</h4>
<p>Thanks for pointing that out. I guess I have to use <code>let base = &amp;self.array as *const MaybeUninit&lt;T&gt;</code>, and then call <code>.add(index)</code> on that then?</p>



<a name="172401707"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172401707" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172401707">(Aug 03 2019 at 10:47)</a>:</h4>
<p>Quoting from the <code>add</code> docs: </p>
<blockquote>
<p>Both the starting and resulting pointer must be either in bounds or one byte past the end of the same allocated object.</p>
</blockquote>



<a name="172401711"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172401711" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172401711">(Aug 03 2019 at 10:47)</a>:</h4>
<p>if you really want this to be safe without bounds checking you need to use <code>wrapping_add</code></p>



<a name="172401712"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172401712" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172401712">(Aug 03 2019 at 10:47)</a>:</h4>
<p>like, <code>self.array.as_ptr().wrapping_add(n)</code></p>



<a name="172401713"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172401713" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172401713">(Aug 03 2019 at 10:47)</a>:</h4>
<p>then you also don't need <code>unsafe</code></p>



<a name="172401756"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172401756" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172401756">(Aug 03 2019 at 10:48)</a>:</h4>
<blockquote>
<p>Because Im not sure what it does?</p>
</blockquote>
<p>it adds 0 to a pointer. not sure how that is special in any way here.</p>



<a name="172401758"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172401758" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172401758">(Aug 03 2019 at 10:48)</a>:</h4>
<p>for an array of size 0 <em>every</em> index is OOB</p>



<a name="172401759"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172401759" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172401759">(Aug 03 2019 at 10:48)</a>:</h4>
<p>but you have OOB problems for all sizes and I dont see how 0 would be special</p>



<a name="172401774"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172401774" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172401774">(Aug 03 2019 at 10:49)</a>:</h4>
<p>oh and you cannot use <code>MaybeUninit::as_ptr</code> for OOB indices either as they take a reference</p>



<a name="172401775"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172401775" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172401775">(Aug 03 2019 at 10:49)</a>:</h4>
<p>and references must not dangle</p>



<a name="172401776"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172401776" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172401776">(Aug 03 2019 at 10:49)</a>:</h4>
<p>I dont get why you dont just do a bounds check, TBH^^</p>



<a name="172401832"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172401832" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> DutchGhost <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172401832">(Aug 03 2019 at 10:51)</a>:</h4>
<p>Well the question really is, if I apply the changes that would make <code>nth_ptr</code> safe,<br>
Then what should <code>first_ptr</code> do for N = 0,<br>
Either returned an aligned pointer, or a pointer to some non-existing value in the array?</p>



<a name="172401946"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172401946" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> DutchGhost <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172401946">(Aug 03 2019 at 10:54)</a>:</h4>
<p>Especially with bounds checking, <code>first_ptr</code> would panic for N = 0.<br>
But this struct is used in an ArrayVec implementation (not the one from <a href="http://crates.io" target="_blank" title="http://crates.io">crates.io</a>), which uses <code>first_ptr</code> in its implementation for Deref&lt;Target = [T]&gt;, so a panic isnt something I'd like</p>



<a name="172401968"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172401968" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172401968">(Aug 03 2019 at 10:55)</a>:</h4>
<p><code>first_ptr(0)</code> should return the address that's 0 away from <code>&amp;self</code>, at a different type</p>



<a name="172401972"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172401972" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172401972">(Aug 03 2019 at 10:55)</a>:</h4>
<p>I dont understand why you think there is a problem here</p>



<a name="172402013"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172402013" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172402013">(Aug 03 2019 at 10:56)</a>:</h4>
<p>it's just like <code>first_ptr(5)</code> returning the address <code>5*size_of::&lt;T&gt;()</code> away from the base, except with 5 replaced by 0...</p>



<a name="172402081"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172402081" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> DutchGhost <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172402081">(Aug 03 2019 at 10:58)</a>:</h4>
<p>Okey, thats cool. I will change all my UB'ness...I really should learn how to avoid all that, thanks for pointing them out! :)</p>



<a name="172402082"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172402082" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172402082">(Aug 03 2019 at 10:58)</a>:</h4>
<p>for <code>N = 0</code>, <code>Self</code> is a ZST. but if you are getting your pr arithmetic right, moving away 5 elements from that pointer, while out-of-bounds, is okay</p>



<a name="172402084"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172402084" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172402084">(Aug 03 2019 at 10:58)</a>:</h4>
<p><span class="user-mention" data-user-id="224097">@DutchGhost</span> as another aid in learning (besides reading the <a href="https://doc.rust-lang.org/nightly/nomicon/" target="_blank" title="https://doc.rust-lang.org/nightly/nomicon/">https://doc.rust-lang.org/nightly/nomicon/</a> ), I can recommend Miri (<a href="https://github.com/rust-lang/miri/" target="_blank" title="https://github.com/rust-lang/miri/">https://github.com/rust-lang/miri/</a>)</p>



<a name="172402093"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172402093" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172402093">(Aug 03 2019 at 10:59)</a>:</h4>
<p>if you run your test suite in it, and if that does an OOB access, it would have caught this</p>



<a name="172402099"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172402099" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172402099">(Aug 03 2019 at 10:59)</a>:</h4>
<blockquote>
<p>so a panic isnt something I'd like</p>
</blockquote>
<p>you <em>are</em> panicking in debug mode though</p>



<a name="172402127"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172402127" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172402127">(Aug 03 2019 at 10:59)</a>:</h4>
<p>so it clearly is incorrect to use that function OOB, and code that does is already broken in debug mode</p>



<a name="172402225"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172402225" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172402225">(Aug 03 2019 at 11:00)</a>:</h4>
<p>either you want OOB accesses, then allow them and document that and be extra-super-careful not to have any (implicit) references anywhere. or you dont want OOB accesses and then I'd recommend checked accesses unless you benchmarked that this is hot code.</p>



<a name="172402823"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172402823" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> DutchGhost <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172402823">(Aug 03 2019 at 11:21)</a>:</h4>
<p>So just to get things straight;<br>
<code>self.array.as_ptr().wrapping_add(index) as *const T</code> is valid, even if index is OOB, so the responsability of the validness of the returned ptr is up to the caller, and  not me</p>



<a name="172403437"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172403437" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172403437">(Aug 03 2019 at 11:42)</a>:</h4>
<p>that code does not even need an <code>unsafe</code> block, so yes, there's no UB there. there cannot be.</p>



<a name="172443478"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172443478" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> DutchGhost <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172443478">(Aug 04 2019 at 10:10)</a>:</h4>
<p>ohh you updated the docs for slice's <code>get_unchecked</code> and <code>get_unchecked_mut</code>! hihi, thats great :)</p>



<a name="172443558"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172443558" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172443558">(Aug 04 2019 at 10:13)</a>:</h4>
<p>well it's not landed yet, but yes :)</p>



<a name="172443609"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array%20pointers/near/172443609" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Array.20pointers.html#172443609">(Aug 04 2019 at 10:14)</a>:</h4>
<p>when questions like this come up, usually to answer them I try to quite the docs, but if the "obvious" docs dont have the quote I need... I just add it ;)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>